home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / dflat_r_.arc / MSGBOX.C < prev    next >
Text File  |  1991-10-02  |  4KB  |  170 lines

  1. /* ------------------ msgbox.c ------------------ */
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <dos.h>
  7. #include <conio.h>
  8. #include "dflat.h"
  9.  
  10. extern DBOX MsgBox;
  11.  
  12. static int ReturnValue;
  13.  
  14. int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  15. {
  16.     switch (msg)    {
  17.         case CREATE_WINDOW:
  18.             GetClass(wnd) = MESSAGEBOX;
  19.             ClearAttribute(wnd, CONTROLBOX);
  20.             break;
  21. #ifdef INCLUDE_DIALOG_BOXES
  22.         case KEYBOARD:
  23.             if (p1 == '\r' || p1 == ESC)
  24.                 ReturnValue = (int)p1;
  25.             break;
  26. #endif
  27.         default:
  28.             break;
  29.     }
  30.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  31. }
  32.  
  33. int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  34. {
  35.     switch (msg)    {
  36.         case CREATE_WINDOW:
  37.             GetClass(wnd) = MESSAGEBOX;
  38.             ClearAttribute(wnd, CONTROLBOX);
  39.             break;
  40.         case KEYBOARD:    {
  41.             int c = tolower((int)p1);
  42.             if (c == 'y')
  43.                 SendMessage(wnd, COMMAND, ID_OK, 0);
  44.             else if (c == 'n')
  45.                 SendMessage(wnd, COMMAND, ID_CANCEL, 0);
  46.             break;
  47.         }
  48. #ifndef INCLUDE_DIALOG_BOXES
  49.         case COMMAND:
  50.             if (p1 == ID_OK || p1 == ID_CANCEL)    {
  51.                 ReturnValue = (int)p1;
  52.                 return TRUE;
  53.             }
  54.             break;
  55. #endif
  56.         default:
  57.             break;
  58.     }
  59.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  60. }
  61.  
  62. int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  63. {
  64.     switch (msg)    {
  65.         case CREATE_WINDOW:
  66.             GetClass(wnd) = ERRORBOX;
  67.             break;
  68. #ifdef INCLUDE_DIALOG_BOXES
  69.         case KEYBOARD:
  70.             if (p1 == '\r' || p1 == ESC)
  71.                 ReturnValue = (int)p1;
  72.             break;
  73. #endif
  74.         default:
  75.             break;
  76.     }
  77.     return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
  78. }
  79.  
  80. int GenericMessage(char *ttl, char *msg, int buttonct,
  81.     int (*wndproc)(struct window *, enum messages, PARAM, PARAM),
  82.     char *b1, char *b2)
  83. {
  84. #ifdef INCLUDE_DIALOG_BOXES
  85.     int rtn;
  86.     MsgBox.dwnd.title = ttl;
  87.     MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
  88.     MsgBox.ctl[0].dwnd.w = max(max(MsgWidth(msg),
  89.             buttonct*8 + buttonct + 2), strlen(ttl)+2);
  90.     MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
  91.     MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
  92.     if (buttonct == 1)
  93.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
  94.     else    {
  95.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
  96.         MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
  97.         MsgBox.ctl[2].class = BUTTON;
  98.     }
  99.     MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
  100.     MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
  101.     MsgBox.ctl[0].itext = msg;
  102.     MsgBox.ctl[1].itext = b1;
  103.     MsgBox.ctl[2].itext = b2;
  104.     MsgBox.ctl[1].isetting = ON;
  105.     MsgBox.ctl[2].isetting = ON;
  106.     rtn = DialogBox(NULLWND, &MsgBox, TRUE, wndproc);
  107.     MsgBox.ctl[2].class = 0;
  108.     return rtn;
  109. #else
  110.     WINDOW wnd;
  111.  
  112.     wnd = CreateWindow(TEXTBOX,ttl,
  113.             -1,-1,MsgHeight(msg)+3,MsgWidth(msg)+2,
  114.             NULL,NULL,
  115.             wndproc,
  116.             HASBORDER | SAVESELF);
  117.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  118.     SendMessage(wnd, SETFOCUS, TRUE, 0);
  119.     SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  120.     SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  121.     ReturnValue = 0;
  122.     while (ReturnValue == 0)
  123.         dispatch_message();
  124.     SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  125.     SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  126.     SendMessage(wnd, CLOSE_WINDOW, 0, 0);
  127.     return ReturnValue == ID_OK || ReturnValue == '\r';
  128. #endif
  129. }
  130.  
  131. WINDOW MomentaryMessage(char *msg)
  132. {
  133.     WINDOW wnd = CreateWindow(
  134.                     TEXTBOX,
  135.                     NULL,
  136.                     -1,-1,MsgHeight(msg)+2,MsgWidth(msg)+2,
  137.                     NULL,NULL,NULL,
  138.                     HASBORDER | SHADOW | SAVESELF);
  139.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  140.     if (cfg.mono == 0)    {
  141.         WindowClientColor(wnd, WHITE, GREEN);
  142.         WindowFrameColor(wnd, WHITE, GREEN);
  143.     }
  144.     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  145.     return wnd;
  146. }
  147.  
  148. int MsgHeight(char *msg)
  149. {
  150.     int h = 1;
  151.     while ((msg = strchr(msg, '\n')) != NULL)    {
  152.         h++;
  153.         msg++;
  154.     }
  155.     return min(h, SCREENHEIGHT-10);
  156. }
  157.  
  158. int MsgWidth(char *msg)
  159. {
  160.     int w = 0;
  161.     char *cp = msg;
  162.     while ((cp = strchr(msg, '\n')) != NULL)    {
  163.         w = max(w, (int) (cp-msg));
  164.         msg = cp+1;
  165.     }
  166.     return min(max(strlen(msg),w), SCREENWIDTH-10);
  167. }
  168.  
  169.  
  170.